package com.difsonapps.android.marker.beans;

import org.json.JSONException;
import org.json.JSONObject;

import com.difsonapps.android.marker.AppConstant;
import com.difsonapps.android.marker.util.MarkerUtil;

import android.os.Parcel;
import android.os.Parcelable;

public class Album  implements Parcelable{
    private int id;
    private int nbPicts;
    private int count_photos;
    private int count_videos;
    private String name, image, desc, timeStamp, created_at, modified_at;
    public boolean facebook;//Means if the album provide from facebook
 
    public Album() {
    }
 
    public Album(int id, String name, String image, int nbPicts,
            String desc, String timeStamp, String url) {
        super();
        this.id = id;
        this.name = name;
        this.image = image;
        this.nbPicts = nbPicts;
        this.desc = desc;
        this.timeStamp = timeStamp;
    }
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getImage() {
        return image;
    }
 
    public void setImage(String image) {
        this.image = image;
    }
 
    public int getNbPicts() {
        return nbPicts;
    }
 
    public void setNbPicts(int nbPicts) {
        this.nbPicts = nbPicts;
    }
    
 
    public int getCount_photos() {
		return count_photos;
	}

	public void setCount_photos(int count_photos) {
		this.count_photos = count_photos;
	}

	public int getCount_videos() {
		return count_videos;
	}

	public void setCount_videos(int count_videos) {
		this.count_videos = count_videos;
	}

	public String getDesc() {
        return desc;
    }
 
    public void setDesc(String desc) {
        this.desc = desc;
    }
    
    
 
    public String getCreated_at() {
		return created_at;
	}

	public void setCreated_at(String created_at) {
		this.created_at = created_at;
	}

	public String getModified_at() {
		return modified_at;
	}

	public void setModified_at(String modified_at) {
		this.modified_at = modified_at;
	}

	public String getTimeStamp() {
        return timeStamp;
    }
 
    public void setTimeStamp(String timeStamp) {
        this.timeStamp = timeStamp;
    }
    

	public boolean isFacebook() {
		return facebook;
	}

	public void setFacebook(boolean facebook) {
		this.facebook = facebook;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Album other = (Album) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public Album(Parcel in){
		this();
		this.id = in.readInt();
		this.nbPicts = in.readInt();
		this.count_photos = in.readInt();
		this.count_videos = in.readInt();
		this.name = in.readString();
		this.image = in.readString();
		this.desc = in.readString();
		this.timeStamp = in.readString();
		this.created_at = in.readString();
		this.modified_at = in.readString();
		this.facebook = (in.readInt()==1);
	}


	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(id);
		dest.writeInt(nbPicts);
		dest.writeInt(count_photos);
		dest.writeInt(count_videos);
		dest.writeString(name);
		dest.writeString(image);
		dest.writeString(desc);
		dest.writeString(timeStamp);
		dest.writeString(created_at);
		dest.writeString(modified_at);
		dest.writeInt(facebook?1:0);
	}	
	
	public static final Parcelable.Creator<Album> CREATOR = new
		Parcelable.Creator<Album>() {
			@Override
			public Album createFromParcel(Parcel source) {
				return new Album(source);
			}
			@Override
			public Album[] newArray(int size) {
				return new Album[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}
	
	public static Album parseFromJsonObject(JSONObject json){
		if(json==null || !json.has("id")) return null;
		Album result = new Album();
		try {
			result.setId(json.getInt("id"));
			result.setNbPicts(json.getInt("nbPicts"));
			result.setCount_photos(json.getInt("count_photos"));
			result.setCount_videos(json.getInt("count_videos"));
			result.setName(json.getString("name"));
			String image = json.isNull("image") ? null : json.getString("image");
			result.setImage(MarkerUtil.buildImageUrl(image, AppConstant.IMAGES_FOLDER, null));
			result.setDesc(json.getString("desc"));
			result.setCreated_at(json.getString("created_at"));
			result.setModified_at(json.getString("modified_at"));
			result.setTimeStamp(json.getString("timeStamp"));
			result.setFacebook(json.has("facebook")&&json.getBoolean("facebook"));
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return result;
	}
}

